package com.eazyshop.goods.service.impl;

import com.eazyshop.goods.dao.BrandMapper;
import com.eazyshop.goods.pojo.Brand;
import com.eazyshop.goods.service.BrandService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;
    /**
     * 品牌列表查询
     * @return
     */
    @Override
    public List<Brand> findList() {
        List<Brand> brands = brandMapper.selectAll();
        return brands;
    }

    @Override
    public Brand findById(Integer id) {

        Brand brand = brandMapper.selectByPrimaryKey(id);
        return brand;
    }

    @Override
    public void addBrand(Brand brand) {
        brandMapper.insertSelective(brand);
    }

    @Override
    public void updateBrand(Brand brand) {
        brandMapper.updateByPrimaryKeySelective(brand);
    }

    @Override
    public void deleteBrandById(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Brand> findListByConditions(Map<String, Object> searchMap) {
        // 创建example
        Example example = new Example(Brand.class);

        Example.Criteria criteria = example.createCriteria();
        // 多条件查询

        // 品牌名字
        if(searchMap.get("name")!=null&&!"".equals(searchMap.get("name"))){
            criteria.andLike("name","%"+searchMap.get("name")+"%");
        }
        //品牌手字母
        if(searchMap.get("letter")!=null&& !"".equals(searchMap.get("letter"))){

            criteria.andEqualTo("letter",searchMap.get("letter"));
        }
        // 返回查询结果
        return brandMapper.selectByExample(example);
    }

    @Override
    public PageInfo<Brand> findByPage(int start, int size) {

        Page<Brand> page = PageHelper.startPage( start, size);
        brandMapper.selectAll();
        PageInfo<Brand> pageInfo = new PageInfo<>(page);
        return pageInfo;
    }

    @Override
    public PageInfo<Brand> findByPageAndConditions(int start, int size, Map<String, Object> conditions) {
        //先分页查询
        Page<Brand> page = PageHelper.startPage( start, size);
        // 条件查询
        Example example = new Example(Brand.class);

        Example.Criteria criteria = example.createCriteria();

        // 设置条件
        if(conditions.get("name")!=null && !"".equals(conditions.get("name"))){

            Object name = conditions.get("name");
            criteria.andLike("name","%"+conditions.get("name")+"%");
        }
        if(conditions.get("letter")!=null && !"".equals(conditions.get("letter"))){

            Object letter = conditions.get("letter");
            criteria.andEqualTo("letter",conditions.get("letter"));
        }
        // 开始查询
        List<Brand> brands = brandMapper.selectByExample(example);


        PageInfo<Brand> pageInfo = new PageInfo<>(page);

        return pageInfo;
    }

    @Override
    public List<Map> findBrandListByCategoryName(String categoryName) {

        List<Map> list = brandMapper.findBrandListByCategoryName(categoryName);

        return list;
    }
}
